//tbClass.Service.ts


import { Inject, Controller, Get, Query, Post, Body, App, All,Provide  } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { HttpService } from "@midwayjs/axios";
import { Application } from "../interface.js";
import { failres, okres } from "../config/resUtil.js";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import {  tbClassEntity  } from "../entity/tbClass.entity.js";
import { BaseDto } from "../models/Base.dto.js";
import { getSkip } from "../utils/pageUtil.js";


@Provide('/api/tb_class')
export class  TbclassService {
  @Inject()
  ctx: Context;
  @App("koa")
  app: Application;

  @InjectEntityModel(tbClassEntity, "default")
  tbClassModel: Repository<tbClassEntity>;

  //find
  async getlist(body: tbClassEntity & BaseDto) {
    var query = this.tbClassModel
      .createQueryBuilder("tb_class").select();
    query=query.where('1=1')




    if (body?.id) {
      query = query.andWhere(`id = :id`,{
        id:body?.id
      });
    }



    if (body?.name) {
      query = query.andWhere(`name = :name`,{
        name:body?.name
      });
    }



    if (body?.parentId) {
      query = query.andWhere(`parent_id = :parentId`,{
        parentId:body?.parentId
      });
    }



    if (body?.typeInfo) {
      query = query.andWhere(`type_info = :typeInfo`,{
        typeInfo:body?.typeInfo
      });
    }





    var result3 = await query.skip(getSkip(body)).take(body.pageSize).getManyAndCount();
    var datalist = (result3?.[0] || []) as unknown as tbClassEntity[];
    return okres(datalist, { total: result3?.[1] });
  }
  //findone
  async getbyid( id) {
    var query = this.tbClassModel
      .createQueryBuilder("tb_class").select();
    query=query.where('1=1')
    query = query.andWhere(`id = :id`,{id});
    var result3 = await query.getOne();
    return okres(result3);
  }
  //add
  async add(body: tbClassEntity & BaseDto) {
    var result =await  this.tbClassModel.save(body)
    return okres(result);
  }
  //update
  async update(@Body() body: tbClassEntity & BaseDto) {
    var result =await  this.tbClassModel.update(body?.id,body)
    return okres(result);
  }
  //delbyid
  async delbyid( id:number) {
    var result =await  this.tbClassModel.delete(id)
    return okres(result);
  }


}

